define(['../../common/service'],function( http){
    var orgInfo = {
        init: function () {
            var _this = this;
            _this.bindEvent();
            _this.initInfo();
        },
        initInfo : function(){
            var _container = $(".org-info-container");
            Kit.loading.show($(_container));
            http.work('/organization/searchOrganization').then(function(res){
                Kit.loading.hide($(_container));
                 if (res.flag == 'success'){
                     var _data = res.data;
                     if (0 != _data.status){
                         //信息已设置
                         $(_container).find("input[name='name']").val(_data.name || '');
                         $(_container).find("input[name='linkMan']").val(_data.linkMan || '');
                         $(_container).find("input[name='linkTel']").val(_data.linkTel || '');
                         $(_container).find("input[name='email']").val(_data.email || '');
                         $(_container).find("textarea[name='address']").val(_data.address || '');
                         _container.find(".init-reminder").addClass("hide");
                         _container.find(".update").removeClass("hide");
                         _container.find(".save").addClass("hide");
                         _container.find(".init").addClass("read-status").attr("readonly", "readonly").removeClass("init");
                     }
                 } else {
                     Kit.alert2(res.msg || I18N.getText('orgInfo','failed'));
                 }
            });
        },
        bindEvent : function(){
            var _this = this;
            //保存
            _this.save();
            //修改
            _this.update();
            _this.vaildate();
        },
        update : function(){
            var _container = $(".org-info-container");
            _container.find(".update").off("click").on("click", function(){
                _container.find(".save").removeClass("hide");
                _container.find(".update").addClass("hide");
                _container.find(".read-status").addClass("init").removeAttr("readonly").removeClass("read-status");
            })
        },
        vaildate:function(){
            var _this = this;
            var _container = $(".org-info-container");
            var allinput = $(_container).find("input");
            allinput.on("input",function(evnet){
                if(allinput.next().text()!=''){
                    var attrname=evnet.currentTarget.name;
                    if(attrname=="name"||attrname=="linkMan"){
                        _this.verify($(evnet.currentTarget))
                    }else if(attrname=="linkTel"){
                        _this.verifyTel($(evnet.currentTarget))
                    }else if(attrname==""){
                        _this.verifyEmail($(evnet.currentTarget))
                    }

                }
            });
            var _address = $(_container).find("textarea[name='address']");
            _address.on("input",function(){
                if(_address.next().text()!=''){
                    _this.verifyAddress(_address)
                }
            });
        },
        save : function(){
            var _this = this;
            var _container = $(".org-info-container");
            _container.find(".save").off("click").on("click", function(){
                var flag = true;
                //校验
                //公司名称
                var _nameD = $(_container).find("input[name='name']");
                flag = _this.verify(_nameD) && flag;

                //联系人
                var _linkMan = $(_container).find("input[name='linkMan']");
                _this.verify(_linkMan);
                flag = _this.verify(_linkMan) && flag;

                //联系电话
                var _linkTel = $(_container).find("input[name='linkTel']");
                flag = _this.verifyTel(_linkTel) && flag;

                //email
                var _email = $(_container).find("input[name='email']");
                flag = _this.verifyEmail(_email) && flag;

                //address
                var _address = $(_container).find("textarea[name='address']");
                flag = _this.verifyAddress(_address) && flag;
                if (!flag){
                    return false;
                }

                //保存
                var par = {
                    name : $.trim(_nameD.val()),
                    linkMan : $.trim(_linkMan.val()),
                    linkTel : $.trim(_linkTel.val()),
                    email : $.trim(_email.val()),
                    address : $.trim(_address.val())
                }

                Kit.loading.show($(_container));
                http.work('/organization/saveOrganization',par,'POST').then(function(res){
                    Kit.loading.hide($(_container));
                    if(res.flag == 'success'){
                        window.localStorage.organizationInfo = JSON.stringify(res.data);
                        _container.find(".init-reminder").addClass("hide");
                        _container.find(".update").removeClass("hide");
                        _container.find(".save").addClass("hide");
                        _container.find(".init").addClass("read-status").attr("readonly", "readonly").removeClass("init");
                    }else{
                        Kit.alert2(res.msg || I18N.getText('orgInfo','failed'));
                    }
                });
            });

        },
        verifyAddress : function(_d){
            var _v = $.trim($(_d).val());
            //长度校验
            var _length = _v.length;
            if (_length <1 || _length > 100){
                _d.addClass("error");
                _d.next(".error-info").html(I18N.getText('orgInfo','addressLimit'));
                return false;
            }
            //特殊字符校验
            var specialCharacter = /^[^。，“”\-\.]*$/;
            var re = new RegExp(specialCharacter);
            if (!re.test(_v)){
                _d.addClass("error");
                _d.next(".error-info").html(I18N.getText('orgInfo','specialChars'));
                return false;
            }
            _d.removeClass("error");
            _d.next(".error-info").html("");
            return true;
        },
        verifyEmail : function(_d){
            var _v = $.trim($(_d).val());
            if ('' == _v){
                _d.addClass("error");
                _d.next(".error-info").html(I18N.getText('orgInfo','emailEmpty'));
                return false;
            }

            var strRegex = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
            var reg = new RegExp(strRegex);
            if (!reg.test(_v)) {
                _d.addClass("error");
                _d.next(".error-info").html(I18N.getText('orgInfo','emailFormatError'));
                return false;
            }
            _d.removeClass("error");
            _d.next(".error-info").html("");
            return true;
        },
        verifyTel : function(_d){
            var _v = $.trim($(_d).val());
            var strRegex = "^[0-9]";
            var lengthRegex="^[0-9]{6,20}$"
            var reg = new RegExp(strRegex);
            var reg2=new RegExp(lengthRegex);
            if (!reg.test(_v) ){
                _d.addClass("error");
                _d.next(".error-info").html(I18N.getText('orgInfo','telFormat'));
                return false;
            } else if (!reg2.test(_v) ){
                _d.addClass("error");
                _d.next(".error-info").html(I18N.getText('orgInfo','telSize'));
                return false;
            }

            _d.removeClass("error");
            _d.next(".error-info").html("");
            return true;
        },
        verify : function(_d){
            var _v = $.trim($(_d).val());
            //长度校验
            var _length = _v.length;
            if (_length <1 || _length > 25){
                _d.addClass("error");
                _d.next(".error-info").html(I18N.getText('orgInfo','lengthLimit'));
                return false;
            }

            //特殊字符校验
            var specialCharacter = /^[^。，“”\-\.]*$/;
            var re = new RegExp(specialCharacter);
            if (!re.test(_v)){
                _d.addClass("error");
                _d.next(".error-info").html(I18N.getText('orgInfo','specialChars'));
                return false;
            }

            _d.removeClass("error");
            _d.next(".error-info").html("");
            return true;
        }
    }

    return orgInfo;
});

